<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 9:28
 */

namespace app\api\controller;

use app\api\Common;
use app\model\CollectionCo;
use app\model\CollectionComment;
use app\model\CollectionCate;
use app\model\CollectionM;
use app\model\CollectionZan;
use app\model\CoCommentZan;
use think\Db;
use think\Request;
use think\Validate;

//视频集锦模块
class Collection extends Common{


    //视频集锦接口
    public function video_collection(){
        $uid = \request()->get('uid');   //获取用户id
        $video_list = CollectionM::where('dstatus',1)->order('id desc')->paginate(6);
        if(!empty($uid)){
            //获取用户收藏的视频id
            $video_id = CollectionCo::where('uid',$uid)->where('status',0)->column('collection_id');
            //获取用户点赞视频id
            $zan_id = CollectionZan::where('uid',$uid)->column('collection_id');
            foreach ($video_list as $k => $v){
                //1为已收藏 0为未收藏
                if(in_array($v['id'],$video_id)){
                    $video_list[$k]['is_collect'] = 1;
                }else{
                    $video_list[$k]['is_collect'] = 0;
                }
                //1为已点赞 0为未点赞
                if(in_array($v['id'],$zan_id)){
                    $video_list[$k]['is_zan'] = 1;
                }else{
                    $video_list[$k]['is_zan'] = 0;
                }
            }
        }else{
            foreach ($video_list as $k => $v){
                //1为已收藏 0为未收藏
                $video_list[$k]['is_collect'] = 0;
                $video_list[$k]['is_zan'] = 0;
            }
        }
        return_json(1,'',$video_list);
    }

    /**
     * 视频详情   ios
     */
    public function video_details(){

        $collection_id = \request()->get('collection_id');   //获取视频集锦id
        $uid = \request()->get('uid');   //用户id
        //获取用户收藏的视频id
        $video_id = CollectionCo::where('uid',$uid)->where('status',0)->column('collection_id');
        //获取用户点赞视频id
        $zan_id = CollectionZan::where('uid',$uid)->column('collection_id');
        $list = CollectionM::where('id',$collection_id)->find();
        if(!empty($uid)){
            if(in_array($list['id'],$video_id)){
                $list['is_collect'] = 1;
            }else{
                $list['is_collect'] = 0;
            }
            if(in_array($list['id'],$zan_id)){
                $list['is_zan'] = 1;
            }else{
                $list['is_zan'] = 0;
            }
        }else{
            $list['is_collect'] = 0;
            $list['is_zan'] = 0;
        }
        return_json(1,'获取成功',$list);

    }

    //视频集锦详情|评论 列表
    public function collection_details(){

        $collection_id = \request()->get('collection_id');   //获取视频集锦id
        $uid = \request()->get('uid');    //获取用户id
        //使用关联查询出视频评论
        $details = CollectionComment::where('collection_id',$collection_id)->with(['getUser'=>function($query){
            $query->field('id,username,head_portrait');
        }])->order('id desc')->paginate(10);

        foreach ($details as $k => $v){
            $v['content'] = urldecode($v['content']);
            if(!empty($uid)){
                //查询用户点赞评论id
                $zan_ids = CoCommentZan::where('uid',$uid)->column('comment_id');
                if(in_array($v['id'],$zan_ids)){
                    $details[$k]['is_zan'] = 1;
                }else{
                    $details[$k]['is_zan'] = 0;
                }
            }else{
                $details[$k]['is_zan'] = 0;
            }
        }
        return_json(1,'获取评论成功',$details);
    }


    //视频集锦点赞|评论点赞
    public function click_collection(){
        //获取视频集锦id|评论id
        $id = \request()->post('id');
        //获取点赞类型 type  1为视频集锦点赞 | 2为评论点赞
        $type = \request()->post('type');
        //获取点赞用户id
        $uid = \request()->post('uid');
        /**
         * 开启实物
         */
        Db::startTrans();
        try{
            if($type==1){
                //判断用户是否已经点赞视频
                $zan_id = CollectionZan::where('uid',$uid)->where('collection_id',$id)->value('id');
                if(!empty($zan_id)){
                    //减少点赞记录数
                    $collection = CollectionM::get($id);
                    $collection->point = $collection->point-1;     //点赞数减1
                    if($collection->save()){
                        //已点赞则为取消|删除点赞记录
                        $status = CollectionZan::destroy($zan_id);
                    }
                }else{
                    $collection = CollectionM::get($id);
                    $collection->point = $collection->point+1;     //点赞数加1
                    if($collection->save()){
                        //添加视频点赞记录s
                        $status = CollectionZan::create([
                            'uid'=>$uid,
                            'collection_id'=>$id
                        ]);
                    }
                }
            }else{
                //判断用户是否已经点赞评论
                $zan_id = CoCommentZan::where('uid',$uid)->where('comment_id',$id)->value('id');
                if(!empty($zan_id)){
                    $comment = CollectionComment::get($id);
                    $comment->like_num = $comment->like_num-1;     //点赞数减1
                    if($comment->save()){
                        $status = CoCommentZan::destroy($zan_id);
                    }
                }else{
                    $comment = CollectionComment::get($id);
                    $comment->like_num = $comment->like_num+1;     //点赞数加1
                    if($comment->save()){
                        //添加评论点赞就记录
                        $status = CoCommentZan::create([
                            'uid'=>$uid,
                            'comment_id'=>$id
                        ]);
                    }

                }
            }
            /**
             * 提交事物
             */
            Db::commit();
            if($status){
                return_json(1,'操作成功');
            }
        }catch (\Exception $e){
            /**
             * 事物回滚
             */
            Db::rollback();
            return_json(0,'操作失败');
        }
    }

    //视频发表评论
    public function review_video(){
        //获取用户id(uid) 视频集锦id(collection_id) 评论内容(content)
        $data = \request()->post();
        $validate=Validate::make([
            'uid'=>"require",
            'collection_id'=>"require",
            'content'=>"require",

        ],[],
            [
                'uid'=>'用户id',
                'collection_id'=>'视频id',
                'content'=>'评论内容'
            ]);
        if(!$validate->check($data)){
            return_json(0,$validate->getError());
        }
        $data['content'] = urlencode($data['content']);
        $data['ctime'] = time();
        $data['mtime'] = time();
        CollectionComment::create($data);
        $collection = CollectionM::get($data['collection_id']);
        $collection->comment = $collection->comment+1;    //评论数加1
        if($collection->save()){
            return_json(1,'评论成功');
        }else{
            return_json(0,'评论失败');
        }
    }


    //用户收藏|取消收藏接口
    public function collection_co(){
        $uid = request()->post('uid');   //获取用户id
        $collection_id = request()->post('collection_id');     //获取视频id

        //判断用户是否已经收藏
        $res = CollectionCo::where('uid',$uid)->where('collection_id',$collection_id)->find();
        if(!empty($res)){
            //已经收藏则为取消
            $res->status = $res->status == 1 ? 0:1;
            $status = $res->save();

        }else{
            $status = CollectionCo::create([
                'collection_id'  =>  $collection_id,
                'uid' =>  $uid,
                'stime'=>time(),
                'status'=>0,
            ]);
        }
        if($status){
            return_json(1,'操作成功');
        }else{
            return_json(0,'操作失败');
        }
    }

}